package echo.nio.byteBuffer;

import java.io.FileInputStream;
import java.io.IOException;
import java.nio.channels.FileChannel;

/**
 * @author： zhangnan
 * @date： 2025/4/20 22:02
 * @description： TODO
 * @modifiedBy：
 * @version: 1.0
 */
public class TestFileChannelTransferTo {

    public static void main(String[] args) throws IOException {
        FileChannel from = new FileInputStream("data.txt").getChannel();
        FileChannel to = new FileInputStream("to.txt").getChannel();
        long size = from.size(); // 效率高，底层会利用操作系统的零拷贝进行优化，最大2g数据
        // left 变量代表还剩余多少字节
        for (long left = size; left > 0;) {
            System.out.println("position: " + (size - left) + " left: " + left);
            left -= from.transferTo((size - left), left,to);
        }
    }


}
